package com.siblings.loan.ui.steps.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.base.base.BaseActivity
import com.lib.base.utils.DensityUtils
import com.siblings.lib_baseadapter.SIBRecyclerViewAdapter
import com.siblings.lib_baseadapter.SIBViewHolderHelper
import com.siblings.loan.R
import com.siblings.loan.app.Apps
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.decoration.LinearSpacingItemDecoration
import com.siblings.loan.utils.dialog.StepCommonPopup
import com.siblings.loan.utils.event.SibEventType
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/11 10:43
 * @Annotation：流程 - 联系人适配器
 */
class SibStepContactAdapter(private val parentView:ViewGroup, recyclerView: RecyclerView?) : SIBRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_step_contact_brand) {
    override fun fillData(helper: SIBViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.tvBrandTitle, "${Apps.app.getString(R.string.step_contact)}${position+1}")

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = DensityUtils.dp2px(14f)
            recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,0,topDecoration))
        }
        val adapter = SibStepCommonAdapter(recyclerView)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            val dataList = arrayListOf<JSONObject>()
            val dataArray = childModel.optJSONArray("values")
            if (dataArray != null) {
                for (i in 0..<dataArray.length()) {
                    dataList.add(dataArray.optJSONObject(i))
                }
            }
            showPopupwindow(childView!!, dataList) { data ->
                childModel.put("selectItem", data)
                adapter.notifyItemChanged(childPosition)
                HttpHelper.addSibEventAction(String.format(SibEventType.CLICK.SIB_CLICK_STEP_XX_SELECT, childModel.optString("paramName")), data.optString("value")
                )
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                dataList.add(dataArray.optJSONObject(i))
            }
        }
        adapter.data = dataList
    }

    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = StepCommonPopup(mContext)
        selectPopup.setWindowWidth(view.width + DensityUtils.dp2px(10f))
        selectPopup.setSelectItemListener(object : StepCommonPopup.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                listener.onChildSelectClick(data!!)
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        selectPopup.show(view, parentView!!)
        selectPopup.setBrandList(dataList)
    }
}